package com.wu.controller;

import com.wu.pojo.Student;
import com.wu.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wsq547 on 2021/10/4 14:31
 */
@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    @Qualifier("StudentServiceImpl")
    private StudentService studentService;



    /*
     * 测试
     * */
    @RequestMapping("/tt1")
    public List<Student> list(){
        List<Student> list = studentService.queryAllStudent();
        System.out.println(list);
        return list;
    }

    /*
    * 展示所有学生,返回到一个学生展示页面*/
    @RequestMapping("/allStudent")
    public String list(Model model){
        List<Student> students = studentService.queryAllStudent();
        model.addAttribute("studentList",students);
        return "showAllBook";
    }
    /*
    *
    * */




    /*
    * 新增学生信息
    * */
    //跳转到增加学生页面
    @RequestMapping("/addStudentPage")
    public String addStudentPage(){
        return"addStudent";
    }

    //增加学生的请求
    @RequestMapping("/addStudent")
    public String addStudent(Student student){
        System.out.println("增加的学生："+student);
        studentService.addStudent(student);
        return "redirect:/student/allStudent";
    }

    /*
    修改学生信息
    * */
    //跳转到修改页面,并展示要修改的信息
    @RequestMapping("/updateStudentPage")
    public String updateStudentPage(Model model,int id){
        Student student = studentService.queryStuedntById(id);
        model.addAttribute("Qstudent",student);
        return "/updateStudent";
    }
    //修改信息的请求
    @RequestMapping("/updateStudent")
    public String updateStudent(Student student){
        studentService.updateStudent(student);
        return "redirect:/student/allStudent";

    }

    /*
    * 删除学生信息
    * */
    @RequestMapping("/deleteStudent")
    public String deleteStudent(int id){
        studentService.deleteStudentById(id);
        return "redirect:/student/allStudent";
    }

    /*
    * 按姓名搜索学生
    * */
    @RequestMapping("/queryStudentByName")
    public String queryStudent(String queryStudentName,Model model){
        Student student = studentService.queryStudentByName(queryStudentName);
        List<Student> list = new ArrayList<Student>();
        list.add(student);

        if(student==null){
            System.out.println("空查询");
            list = studentService.queryAllStudent();
            model.addAttribute("error","未查到");

        }

        model.addAttribute("studentList",list);
        return "showAllBook";

    }



}
